package com.yy.bolg.controller.deviceToDetection;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yy.bolg.constant.ResultCode;
import com.yy.bolg.entity.CustomerBean;
import com.yy.bolg.entity.DeviceToDetectionBean;
import com.yy.bolg.request.CustomerRequest;
import com.yy.bolg.request.DeviceToDetectionRequest;
import com.yy.bolg.request.statistics.admin.CustomerRegionStatisticsRequest;
import com.yy.bolg.resp.CustomerResp;
import com.yy.bolg.resp.DeviceToDetectionResp;
import com.yy.bolg.resp.statistics.admin.CustomerRegionStatisticsResp;
import com.yy.bolg.service.DeviceToDetectionService;
import com.yy.bolg.util.ChongQingRegionUtil;
import com.yy.bolg.util.CodeGenerateUtil;
import com.yy.bolg.util.RespenseJsonTransformationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 设备和检测项关联控制器
 * @author enmaoFu
 * @date 2025-04-06
 * RequestMapping("/dtd") 是说明这个类的访问地址是 /dtd
 */
@Api(tags = {"设备和检测项关联"})
@Controller
@RequestMapping(value = "/dtd")
public class DeviceToDetectionController {

    @Autowired
    private DeviceToDetectionService service;// 自动载入Service对象

    /**
     * 添加
     * 服务器访问路径：http://localhost:8080/bolg/dtd/add
     * 本地访问路径：http://localhost:8111/dtd/add
     * 参数：
     * {"ddDeviceId":"设备ID","ddDetectionId":"检测项ID","ddDetectionName":"检测项别名"}
     */
    @ApiOperation(value = "添加", notes = "参数：{\"ddDeviceId\":\"设备ID\",\"ddDetectionId\":\"检测项ID\",\"ddDetectionName\":\"检测项别名\"}")
    @RequestMapping(value = "/add",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String add(@RequestBody DeviceToDetectionRequest request){
        String result;

        DeviceToDetectionBean deviceToDetectionBean = new DeviceToDetectionBean();
        deviceToDetectionBean.setPsDdDeviceId(request.getDdDeviceId());
        deviceToDetectionBean.setPsDdDetectionId(request.getDdDetectionId());
        deviceToDetectionBean.setPsDdDetectionName(request.getDdDetectionName());

        try {
            service.add(deviceToDetectionBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "添加成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 修改别名
     * 服务器访问路径：http://localhost:8080/bolg/dtd/up
     * 本地访问路径：http://localhost:8111/dtd/up
     * 参数：{"ddDetectionId":"检测项ID","ddNewDetectionName":"检测项新别名"}
     */
    @ApiOperation(value = "修改别名", notes = "参数：{\"ddDetectionId\":\"检测项ID\",\"ddNewDetectionName\":\"检测项新别名\"}")
    @RequestMapping(value = "/up",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String upCustomerUserInfo(@RequestBody DeviceToDetectionRequest request){
        String result;

        DeviceToDetectionBean deviceToDetectionBean = new DeviceToDetectionBean();
        deviceToDetectionBean.setPsDdDetectionId(request.getDdDetectionId());
        deviceToDetectionBean.setPsDdNewDetectionName(request.getDdNewDetectionName());

        try {
            service.up(deviceToDetectionBean);

            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "修改成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 删除
     * 服务器访问路径：http://localhost:8080/bolg/dtd/delete
     * 本地访问路径：http://localhost:8111/dtd/delete
     * 参数：{"ddDetectionId":"检测项ID"}
     */
    @ApiOperation(value = "删除", notes = "参数：{\"ddDetectionId\":\"检测项ID\"}")
    @RequestMapping(value = "/delete",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String delete(@RequestBody DeviceToDetectionRequest request){
        String result;

        DeviceToDetectionBean deviceToDetectionBean = new DeviceToDetectionBean();
        deviceToDetectionBean.setPsDdDetectionId(request.getDdDetectionId());

        try {
            service.del(deviceToDetectionBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "删除成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 根据检测项ID获取数据（一条）
     * 服务器访问路径：http://localhost:8080/bolg/dtd/getDtdInfo
     * 本地访问路径：http://localhost:8111/dtd/getDtdInfo
     * 参数：{"ddDetectionId":"检测项ID"}
     */
    @ApiOperation(value = "根据检测项ID获取数据（一条）", notes = "参数：{\"ddDetectionId\":\"检测项ID\"}", response = CustomerResp.class)
    @RequestMapping(value = "/getDtdInfo",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String getDtdInfo(@RequestBody DeviceToDetectionRequest request){
        String result;

        DeviceToDetectionBean deviceToDetectionBean = new DeviceToDetectionBean();
        deviceToDetectionBean.setPsDdDetectionId(request.getDdDetectionId());

        try {
            DeviceToDetectionBean returnBean = service.findOneByDetectionId(deviceToDetectionBean);
            if (null != returnBean) {
                DeviceToDetectionResp resp = new DeviceToDetectionResp();
                resp.setDdDeviceId(returnBean.getPsDdDeviceId());
                resp.setDdDetectionId(returnBean.getPsDdDetectionId());
                resp.setDdDetectionName(returnBean.getPsDdDetectionName());

                Map map = new HashMap();
                map.put("data", resp);
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "获取信息成功", map);
            } else {
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL,"暂无数据",new HashMap());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL,e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 根据设备ID查询所有数据
     * 服务器访问路径：http://localhost:8080/bolg/dtd/findAllById
     * 本地访问路径：http://localhost:8111/dtd/findAllById
     * 参数：{"ddDeviceId":"设备ID"}
     */
    @ApiOperation(value = "根据设备ID查询所有数据", notes = "参数：{\"ddDeviceId\":\"设备ID\"}", response = CustomerResp.class)
    @RequestMapping(value = "/findAllById",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String findAllById(@RequestBody DeviceToDetectionRequest request){
        String result;

        DeviceToDetectionBean deviceToDetectionBean = new DeviceToDetectionBean();
        deviceToDetectionBean.setPsDdDeviceId(request.getDdDeviceId());

        try {
            List<DeviceToDetectionResp> respList = new ArrayList<>();
            for(DeviceToDetectionBean returnBean : service.findAllById(deviceToDetectionBean)){
                DeviceToDetectionResp resp = new DeviceToDetectionResp();
                resp.setDdDeviceId(returnBean.getPsDdDeviceId());
                resp.setDdDetectionId(returnBean.getPsDdDetectionId());
                resp.setDdDetectionName(returnBean.getPsDdDetectionName());

                respList.add(resp);
            }

            Map map = new HashMap();
            map.put("data", respList);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }
}
